package com.ymatou.iapi.productquery.compare;

import java.util.List;
import java.util.function.BiFunction;

public class SearchCompare {
	private static <T> boolean compareTplAsList(T t , BiFunction<T,Boolean,List<ProductReturnBean>> function){
		boolean ret = true;
		
		List<ProductReturnBean> productIdIsOKList = function.apply(t, false);
		if (productIdIsOKList!=null){
			for(ProductReturnBean productIdIsOK:productIdIsOKList){
				ret = ret && productIdIsOK.isCompareOk();
				if (!productIdIsOK.isCompareOk()){
					SearchCompareHelper.callSyc(productIdIsOK.getProductId());
				}
			}
		}
		
		if(!ret){
			ret = true;
			List<ProductReturnBean> productIdIsOKList2 = function.apply(t, true);
			if (productIdIsOKList2!=null){
				for(ProductReturnBean productIdIsOK:productIdIsOKList2){
					ret = ret && productIdIsOK.isCompareOk();
				}
			}
		}
		
		return ret;
	}
	
	private static <T> boolean compareTpl(T t ,BiFunction<T,Boolean,ProductReturnBean> function){
		boolean ret = false;
		ProductReturnBean productIdIsOK = function.apply(t,false);
		if (productIdIsOK!=null){
			ret = productIdIsOK.isCompareOk();
			if (!productIdIsOK.isCompareOk()){
				SearchCompareHelper.callSyc(productIdIsOK.getProductId());
				ret = function.apply(t,true).isCompareOk();
			}
		}
		return ret;
	}
	
	private static <T,V> boolean compareTpl(T t ,V v, TFunction<T,V,Boolean,ProductReturnBean> function){
		boolean ret = false;
		ProductReturnBean productIdIsOK = function.apply(t,v,false);
		if (productIdIsOK!=null){
			ret = productIdIsOK.isCompareOk();
			if (!productIdIsOK.isCompareOk()){
				SearchCompareHelper.callSyc(productIdIsOK.getProductId());
				ret = function.apply(t,v,true).isCompareOk();
			}
		}
		return ret;
	}
	
	//多物流购物车接口 目前只支持单个规格比较
	public static boolean compareGetCatalogListByDeliveryExtra(List<String> catalogs,boolean isTradeIsolation){
		return compareTpl(catalogs,isTradeIsolation,(x,y,z)->SearchCompareCore.coreCompareCatalogListByDeliveryExtra(x,y,z));
	}
	
	//购物车接口 目前只支持单个规格比较
	public static boolean compareGetCatalogList(List<String> catalogs,boolean isTradeIsolation){
		return compareTpl(catalogs,isTradeIsolation,(x,y,z)->SearchCompareCore.coreCompareCatalogList(x,y,z));
	}
	
	//单品详情接口
	public static boolean compareGetProductInfo(String productId,boolean isTradeIsolation){
		return compareTpl(productId,isTradeIsolation,(x,y,z)->SearchCompareCore.coreCompareProductInfo(x,y,z));
	}
	
	//列表详情接口
	public static boolean compareGetProductInfoList(List<String> productList,boolean isTradeIsolation){
		return compareTpl(productList,isTradeIsolation,(x,y,z)->SearchCompareCore.coreCompareProductInfoList(x,y,z));
	}
	
	//列表简表接口
	public static boolean compareGetProductInList(List<String> productList,boolean isTradeIsolation){
		return compareTpl(productList,isTradeIsolation,(x,y,z)->SearchCompareCore.coreCompareProductListList(x,y,z));
	}
	
	//历史商品接口
	public static boolean compareProductHistoryList(List<String> productList) {
		return compareTpl(productList,(x,y)->SearchCompareCore.coreCompareProductHistoryList(x,y));
	}
	
	//图文详情接口
	public static boolean compareProductDescExtra(String productId){
		return compareTpl(productId,(x,y)->SearchCompareCore.coreCompareProductDescExtra(x,y));
	}
	
	
	//取秒杀商品的活动库存量
	public static boolean compareSecKillProductActivityStock(String productId,String activityId)  {
		boolean ret = false;
		ProductReturnBean productIdIsOK = SearchCompareCore.coreCompareSecKillProductActivityStock(productId, activityId, false);
		if (productIdIsOK!=null){
			ret = productIdIsOK.isCompareOk();
			if (!productIdIsOK.isCompareOk()){
				SearchCompareHelper.callSyc_new(productIdIsOK.getActivityId(),productIdIsOK.getProductId(),"SyncActivityProduct");
				ret =  SearchCompareCore.coreCompareSecKillProductActivityStock(productId, activityId, true).isCompareOk();
			}
		}
		return ret;
	}
	
	//need test
	//===============================================================================================
	//取买手新品列表接口
	public static boolean compareProductInListForNew(String sellerId) {
		return compareTplAsList(sellerId,(x,y)->SearchCompareCore.coreCompareProductInListForNew(x,y));
	}
	
	//取橱窗商品
	public static boolean compareProductInListForHot(String sellerId){
		return compareTplAsList(sellerId,(x,y)->SearchCompareCore.coreCompareProductInListForHot(x,y));
	}
	
	//买手推荐的商品列表
	public static boolean compareRecommendProductList(List<String> sellerIdList){
		return compareTplAsList(sellerIdList,(x,y)->SearchCompareCore.coreCompareRecommendProductList(x,y));
	}
	

	//直播置顶查询
	public static boolean compareTopProductInLive(String liveId) {
		SearchCompareCore.coreCompareTopProductInLive(liveId, true);
		return true;
	}
	

	
	
	
	
	
	
	
	
	
	
		
}
